﻿using FlexFlow.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexFlow.Runtime
{
    /// <summary>
    /// 表示通知管理器
    /// </summary>
    public class NotificationManager : ManagerBase
    {

        /// <summary>
        /// 获取或设置默认的通知管理器
        /// </summary>
        public static NotificationManager Default { get; set; }

        /// <summary>
        /// 取得指定用户的通知文件夹列表
        /// </summary>
        /// <param name="uid">
        /// 用户ID
        /// </param>
        /// <param name="limit">
        /// 要取得的文件夹数量
        /// </param>
        /// <param name="allCount">
        /// 用户通知文件夹总数量
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public OperateResult GetUserCatalogs(Guid uid, int limit, out long allCount)
        {
            var res = this.Components.NotificationCatalogProvider.GetUserCatalogs(uid, limit, out allCount);
            foreach (var catalog in res)
            {
                //校正通知数量
                if (catalog.LastCountTime < catalog.LastTrigerTime)
                {
                    int catalogItemCount = this.Components.NotificationProvider.Count(catalog.Identity, catalog.LastReadTime);
                    if (this.Components.NotificationCatalogProvider.SetCount(catalog.Identity, catalogItemCount))
                    {
                        catalog.Count = catalogItemCount;
                    }
                }
            }
            return OperateResult.SuccessResult(res);
        }

        /// <summary>
        /// 向指定用户发送
        /// </summary>
        /// <param name="catalog">
        /// 通知所在的文件夹
        /// </param>
        /// <param name="title">
        /// 通知的标题
        /// </param>
        /// <param name="content">
        /// 通知的内容
        /// </param>
        /// <param name="tag">
        /// 通知的附加信息
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public OperateResult SendNotification(NotificationCatalog catalog, string title, string content,Object tag=null)
        {
            NotificationCatalog catalogDB =
                this.Components.NotificationCatalogProvider.Get(catalog.Sender, catalog.Type, catalog.Reciver);
            if (catalogDB == null)
            {
                if (!this.Components.NotificationCatalogProvider.Add(catalog))
                {
                    return new OperateResult()
                    {
                        ResultCode = 0x060101,
                        Message = "创建通知目录失败！"
                    };
                }
                catalogDB = catalog;
            }

            Notification notification = this.Components.NotificationProvider.Create();
            notification.Catalog = catalogDB.Identity;
            notification.Content = content;
            notification.Title = title;
            notification.Reciver = catalogDB.Reciver;
            notification.Sender = catalogDB.Sender;
            notification.Tag = tag;

            if (!this.Components.NotificationProvider.Add(notification))
            {
                return new OperateResult()
                {
                    ResultCode = 0x060102,
                    Message = "添加消息失败！"
                };
            }
            if (!this.Components.NotificationCatalogProvider.UpdateTrigger(catalogDB.Identity, DateTime.Now))
            {
                return new OperateResult()
                {
                    ResultCode = 0x060103,
                    Message = "设置文件夹TriggerTime失败！"
                };
            }

            return OperateResult.SuccessResult(notification.Identity);
        }

        /// <summary>
        /// 初始化通知管理器静态变量
        /// </summary>
        static NotificationManager()
        {
            Default = new NotificationManager();
        }

    }
}
